package test.lucene.pinyin;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;

import test.lucene.DataImport;

public class App2 {
	
	static String path = DataImport.class.getResource("").getPath().split("target")[0] + "data2";
	
	public static void index(int id, String text) {
		IndexWriter writer = null;	
		FSDirectory fsd = null;
		try {
			fsd = FSDirectory.open(Paths.get(new File(path).toURI()));
			Analyzer analyzer = new MyAnalyzer();
			IndexWriterConfig conf = new IndexWriterConfig(analyzer);
			writer = new IndexWriter(fsd, conf);
			Document doc = new Document();
			doc.add(new IntField("id", id, Store.YES));
			doc.add(new TextField("text", text, Store.YES));
			writer.addDocument(doc);
			writer.commit();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				writer.close();
				fsd.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void query(String text) {
		DirectoryReader reader = null;	
		FSDirectory fsd = null;
		try {
			fsd = FSDirectory.open(Paths.get(new File(path).toURI()));
			reader = DirectoryReader.open(fsd);
			IndexSearcher searcher = new IndexSearcher(reader);
			TopDocs docs = searcher.search(new TermQuery(new Term("text", text)), 10);
			for(ScoreDoc doc : docs.scoreDocs) {
				System.out.println(reader.document(doc.doc));
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
				fsd.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void deleteAll() {
		IndexWriter writer = null;	
		FSDirectory fsd = null;
		try {
			fsd = FSDirectory.open(Paths.get(new File(path).toURI()));
			IndexWriterConfig conf = new IndexWriterConfig(new SimpleAnalyzer());
			writer = new IndexWriter(fsd, conf);
			writer.deleteAll();
			writer.commit();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				writer.close();
				fsd.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void fuzzyQuery() {
		DirectoryReader reader = null;	
		FSDirectory fsd = null;
		try {
			fsd = FSDirectory.open(Paths.get(new File(path).toURI()));
			reader = DirectoryReader.open(fsd);
			IndexSearcher searcher = new IndexSearcher(reader);
			TopDocs docs = searcher.search(new FuzzyQuery(new Term("text", "jintian*")), 10);
			for(ScoreDoc doc : docs.scoreDocs) {
				System.out.println(reader.document(doc.doc));
				System.out.println(doc.score);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
				fsd.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
//		deleteAll();
//		index(4, "我们今天去哪吃饭呢");
//		query("jintian");
		fuzzyQuery();
	}

}
